﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 打开
{
    //窗口隐藏部分
    partial class Form1 
    {

        /// <summary>时钟周期间隔</summary>
        public int Interval = 5;

        /// <summary>窗口隐藏许可</summary>
        public bool Allow = false;

        /// <summary>窗口隐藏偏置</summary>
        public offset OffsetHide;

        /// <summary>鼠标窗口偏置</summary>
        public offset OffsetInside;

        /// <summary>窗口隐藏尺寸</summary>
        public Size FormSize;

        /// <summary>窗口偏置变量</summary>
        public struct offset
        {
            public int Top;
            public int Bottom;
            public int Left;
            public int Right;

            public offset(int top,int bottom,int left,int right)
            {
                Top = top;
                Bottom = bottom;
                Left = left;
                Right = right;
            }

            public offset(offset offset)
            {
                Top = offset.Top;
                Bottom = offset.Bottom;
                Left = offset.Left;
                Right = offset.Right;
            }

            public static offset Zero
            {
                get
                {
                    return new offset(0, 0, 0, 0);
                }
            }

            public static offset operator +(offset b, offset c)
            {
                return new offset(b.Top + c.Top, b.Bottom + c.Bottom, b.Left + c.Left, b.Right + c.Right);
            }

        }

        //声明时钟
        private Timer timer = new Timer();
        
        //声明屏幕尺寸
        private Size ScreenSize = new Size(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);

        /// <summary>初始化隐藏窗口</summary>
        public void HideForm()
        {
            FormSize = new Size(Size.Width, Size.Height);       
            timer.Tick += new EventHandler(timer_tick);
            timer.Interval = Interval;
            OffsetHide = new offset(-31,-39,-1,-15);
            OffsetInside = new offset(0, -5, 7, -11);
            timer.Enabled = true;
        }

        

        /// <summary>时钟周期事件</summary>
        private void timer_tick(object sender, EventArgs e)
        {

            /*鼠标在窗口内?*/
            bool inside = isInside(OffsetInside);

            /*窗口是否隐藏?*/
            //bool ishide = isHide();

            //判断准许隐藏或显示窗口
            if (inside)
            {
                if (MouseButtons == MouseButtons.Left)
                {
                    return;
                }
                Allow = true;
                showForm();
            }
            else if(Allow)
            {
                hideForm();
            }

        }

        /// <summary>判断鼠标是否在窗口内</summary>
        private bool isInside(offset offset)
        {
            //获取鼠标位置
            Point point = PointToClient(MousePosition);

            //获取窗口区域（带偏移量）
            Rectangle rect = RectangleToClient(new Rectangle(
                                                                Location.X + offset.Left, Location.Y + offset.Top, 
                                                                Size.Width + offset.Right, Size.Height + offset.Bottom
                                                             )
                                               );

            return rect.Contains(point);

        }

        /// <summary>判断窗口是否隐藏</summary>
        private bool isHide()
        {
            //鼠标区域各边向内偏置2像素
            offset offset = OffsetInside + new offset(2, -2, 4, -8);

            //偏置后的窗口区域
            Rectangle rect_offset = new Rectangle(
                                                    Location.X + offset.Left, Location.Y + offset.Top,
                                                    Size.Width + offset.Right, Size.Height + offset.Bottom
                                                 );
                                              
            //取屏幕区域
            Rectangle recet_screen = new Rectangle(new Point(0, 0), ScreenSize);

            return !recet_screen.Contains(rect_offset);

        }

        /// <summary>显示窗口</summary>
        private void showForm()
        {
            TopLevel = true;

            /*窗口在左边*/
            if (this.Left < -7 && this.Right >= 0)
                if (this.Left + 45 > 0) this.Left += 1;
                else this.Left += 35;
            /*窗口在顶边*/
            if (this.Top < 0 && this.Bottom >= 0)
                if (this.Top + 45 > 0) this.Top += 1;
                else this.Top += 35;
            /*窗口在右边*/
            if (this.Left <= ScreenSize.Width - 5 && this.Right > ScreenSize.Width + 7)
                if (this.Right - 45 < ScreenSize.Width) this.Left -= 1;
                else this.Left -= 35;

        }

        /// <summary>显示窗口</summary>
        private void showForm(bool simple)
        {
            Allow = false;

            int width = Screen.PrimaryScreen.Bounds.Width;

            TopLevel = true;

            if (this.Left < 0 && this.Right > 0)
            {
                this.Left = -7;
            }
            if (this.Right > width && this.Left < width)
            {
                this.Left = width - this.Width;
            }
            if (this.Top < 0 && this.Bottom > 0)
            {
                this.Top = 0;
            }

        }

        /// <summary>隐藏窗口</summary>
        private void hideForm()
        {

            /*窗口在左边*/
            if (this.Left <= 0 && this.Right > 8)
                if (this.Right - 45 < 0) this.Left -= 1;
                else this.Left -= 35;

            /*窗口在顶边*/
            if (this.Top <= 0 && this.Bottom > 8)
                if (this.Bottom - 45 < 0) this.Top -= 1;
                else this.Top -= 35;

            /*窗口在右边*/
            if (this.Left < ScreenSize.Width - 8 && this.Right >= ScreenSize.Width)
                if (this.Left + 41 > ScreenSize.Width) this.Left += 1;
                else this.Left += 35;

        }
    }

}
